//add by yuanfei
#include <map.h>
#include "cocos2d.h"
#include "HttpClient.h"

using namespace cocos2d::extension;
using namespace std;

class HttpHandler : public CCObject{
public:
  HttpHandler(){
  }
  void onHttpRequestCompleted(cocos2d::CCNode *sender, void *data){
    CCHttpResponse *response = (CCHttpResponse*)data;
    if (!response){
      return;
    }
    const char* tag = response->getHttpRequest()->getTag();
    CCLog("%s completed ", tag);

    vector<char> *charData = response->getResponseData();
    string res;
    res.assign(charData->begin(), charData->end());
    CCLog("completed %p, %d, %s", response->getHttpRequest()->getUserData(), res.size(), "");
    //sleep(2); 
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
    pEngine->executeFunctionWithString(response->getHttpRequest()->getUserData(), tag, res.c_str());
  }
};

HttpHandler httpHandler;
static int tolua_Cocos2d_CCHttpClient_request(lua_State* tolua_S)
{
  const char* url = ((const char*)  tolua_tostring(tolua_S,2,0));
  const char* callback = ((const char*)  tolua_tostring(tolua_S,3,0));
  const char* postdata = ((const char*)  tolua_tostring(tolua_S,4,0));
  CCArray* tmpHeader = ((CCArray*)  tolua_tousertype(tolua_S,5,0));
 
  {
    CCHttpRequest* request = new CCHttpRequest();
    request->setUrl(url);
    if(postdata){
      //CCLog("##set post data %s %d", postdata, strlen(postdata));
      request->setRequestType(CCHttpRequest::kHttpPost);
      request->setRequestData(postdata, strlen(postdata));
    }else{
      request->setRequestType(CCHttpRequest::kHttpGet);
    }
    std::vector<std::string> headers;
    if(tmpHeader==NULL){
      headers.push_back("Content-Type: text");
    }else{
      for(int i=0; i<tmpHeader->count(); i++){
        CCString *h = (CCString*)tmpHeader->objectAtIndex(i);
        headers.push_back(h->getCString());
      }
    }
    string tmp = url;
    int pos = tmp.find("/", 8);
    tmp = tmp.substr(7, pos-7);
    headers.push_back("Host: "+tmp);
    //headers.push_back("User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36");
    CCLog("tolua_Cocos2d_CCHttpClient_request, %d %s", pos, tmp.c_str());
    //headers.push_back("Content-Type: application/x-www-form-urlencoded");
    for(int i=0; i<headers.size(); i++){
      CCLog("header --> %s", headers.at(i).c_str());
    }
    request->setHeaders(headers);

    request->setResponseCallback(&httpHandler, callfuncND_selector(HttpHandler::onHttpRequestCompleted));
    request->setTag(callback);
    request->setUserData(request);
    CCHttpClient::getInstance()->send(request);
    request->release();
    CCLog("request %p %s %s %s", request, url, callback, postdata);

    //CCObject* obj = (CCObject*)  tolua_tousertype(tolua_S,1,0);
    int ret = lua_istable(tolua_S, -1);
    //printf("request is table %d\n", ret);
    if(ret){
      lua_getglobal(tolua_S, "ObjectExtTable");
      char key[64];
      sprintf(key, "userdata: %p", request);
      lua_pushstring(tolua_S, key);
      lua_pushvalue(tolua_S, -3);
      lua_settable(tolua_S, -3);
      lua_pop(tolua_S, 1);
    }
  }

  return 1;
}


